package nanfang.nanf.dao.impl;

import nanfang.nanf.DBUtil.DButil;
import nanfang.nanf.dao.StudentDAO;
import nanfang.nanf.entity.Student;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class StudentImpl implements StudentDAO {
    @Override
    public List<Student> AllStudent() {
        List<Student> list = new ArrayList<>();
        Connection conn = DButil.getConnection();
        String sql = "select * from staffs";
        PreparedStatement pst = null;
        try {
            pst = conn.prepareStatement(sql);
            ResultSet rst = pst.executeQuery();
            while (rst.next()){
                int id = rst.getInt("Id");
                String name = rst.getString("Name");
                int age = rst.getInt("age");
                String phone = rst.getString("phone");
                String pos = rst.getString("sta_pos");
                String addTime = rst.getString("add_time");
                String updateTime = rst.getString("update_time");
                Student good = new Student(id,name,age,phone,pos, addTime,updateTime);
                list.add(good);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }

    @Override
    public int addStudent(Student Stu) {
        int count = 0;
        Connection conn = DButil.getConnection();
        String sql = "insert into staffs(name,age,phone,sta_pos) values (?,?,?,?)";
        PreparedStatement pst = null;
        ResultSet rs = null;
        try {
            pst = conn.prepareStatement(sql);
            pst.setString(1,Stu.getName());
            pst.setInt(2,Stu.getAge());
            pst.setString(3,Stu.getPhone());
            pst.setString(4, Stu.getStaPos());
            count  = pst.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return count;
    }

    @Override
    public int delStudent(int id) {
        int count = 0;
        Connection conn =DButil.getConnection();
        String sql = "delete  from staffs where id = ?";
        PreparedStatement pst = null;
        try {
            pst = conn.prepareStatement(sql);
            pst.setString(1, String.valueOf(id));
            count = pst.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return count;
    }

    @Override
    public int UpdaStudent(Student stu) {
        int row = 0 ;
        Connection conn = DButil.getConnection();
        String sql = "update staffs set name = ?,age = ?,phone = ?,sta_pos = ?,update_time = ? where id = ?";
        PreparedStatement pst = null;
        ResultSet rs = null;
        try {
            pst = conn.prepareStatement(sql);
            pst.setString(1, stu.getName());
            pst.setInt(2, stu.getAge());
            pst.setString(3, stu.getPhone());
            pst.setString(4, stu.getStaPos());
            pst.setString(5, stu.getUpdateTime());
            pst.setInt(6, stu.getId());
           row = pst.executeUpdate();

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return row ;
    }




    public static void main(String[] args) {
//        展示所有
//        System.out.println(new StudentImpl().AllStudent());
        //添加
//        System.out.println(new StudentImpl().addStudent(new Student("xia",18,"154422222","经理")));

//        删除
//        System.out.println(new StudentImpl().delStudent(1));

//        修改
//        staffs staff = new staffs("1","张三3","20","123456789","经理","2025-02-24","2025-02-24");
//        System.out.println(new staffsDaoImpl().updateStaff(staff));
    }
}
